<?php

namespace app\merchant\controller\store;

use app\merchant\controller\Base;
use app\merchant\model\store\User as StoreUserModel;
use app\merchant\model\store\UserRole;
/**
 * 商家管理员控制器
 * Class StoreUser
 * @package app\merchant\controller\store
 */
class User extends Base{

    /**
     * 管理员列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new StoreUserModel;
        $list = $model->getList();

        return $this->renderSuccess(compact('list'));

    }


    /**
     * 添加管理员
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function add()
    {
        //获取数据
        $param = $this->postData();

        $model = new StoreUserModel;
        // 新增记录
        if ($model->add($param)) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError([],$model->getError() ?: '操作失败');

    }


    /**
     * 获取管理员详情
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        //获取数据
        $param = $this->postData();
        // 管理员详情
        $datail = StoreUserModel::detail($param['store_user_id']);
        if(!$datail){
            return $this->renderError([],'管理员不存在');
        }
        $datail['roleIds'] = UserRole::getRoleIds($datail['store_user_id']);

        return $this->renderSuccess($datail,'查询成功');

    }

    /**
     * 编辑管理员
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        //获取数据
        $param = $this->postData();
        if(!isset($param['store_user_id']) || isInt($param['store_user_id'])){
            return $this->renderError([],'参数错误');
        }
        // 管理员详情
        $model = StoreUserModel::detail($param['store_user_id']);
        if(!$model){
            return $this->renderError([],'管理员不存在');
        }
        // 新增记录
        if ($model->edit($param)) {
            return $this->renderSuccess([],'编辑成功');
        }
        return $this->renderError([],$model->getError() ?: '操作失败');
    }



    /**
     * 删除管理员
     * @param $user_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        //获取数据
        $param = $this->postData();

        if(!isset($param['store_user_id']) || isInt($param['store_user_id'])){
            return $this->renderError([],'参数错误');
        }
        // 管理员详情
        $model = StoreUserModel::detail($param['store_user_id']);

        if(!$model){
            return $this->renderError([],'管理员不存在');
        }
        if (!$model->setDelete()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');

    }
}